package cn.yujiago.miaosha.controller;

import cn.yujiago.miaosha.component.UserContext;
import cn.yujiago.miaosha.dto.GoodsDTO;
import cn.yujiago.miaosha.dto.GoodsDetailDTO;
import cn.yujiago.miaosha.dto.Result;
import cn.yujiago.miaosha.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/detail")
    public Result<GoodsDetailDTO> getGoodsDetail(@RequestParam("goodsId") Long goodsId){

        GoodsDTO goods = goodsService.getGoodsDetail(goodsId);
        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();
        int miaoshaStatus = 0;
        if(now < startAt ) {// 秒杀还没开始，倒计时
            miaoshaStatus = 0;
        }else  if(now > endAt){// 秒杀已经结束
            miaoshaStatus = 2;
        }else {// 秒杀进行中
            miaoshaStatus = 1;
        }

        GoodsDetailDTO vo = new GoodsDetailDTO();
        vo.setGoods(goods);
        vo.setUser(UserContext.getUser());
        vo.setCurrentTimeMillis(System.currentTimeMillis());
        vo.setMiaoshaStatus(miaoshaStatus);
        return Result.success(vo);
    }

}
